<?php

namespace frontend\models;

use common\models\Apply;
use Yii;
use yii\base\Model;

/**
 * ContactForm is the model behind the contact form.
 */
class ApplyForm extends Model
{
    public $content;
    public $name;


    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            // name, email, subject and body are required
            ['name', 'required','message'=>'请输入分类名'],
            ['name', 'unique', 'targetClass' => '\common\models\Apply', 'message' => '分类已存在'],
            ['content', 'required','message'=>'你输入申请理由'],
            ['content', 'safe'],
            [['content'], 'string', 'min' => 5,'message'=>'举报理由至少5个字符'],

        ];
    }


    /**
     * Sends an email to the specified email address using the information collected by this model.
     *
     * @param  string  $email the target email address
     * @return boolean whether the email was sent
     */
    public function addReplay()
    {
        if($this->validate()){
            $apply = new Apply();
            $apply->name = htmlspecialchars($this->content);
            $apply->content = htmlspecialchars($this->content);
            $apply->user_id = Yii::$app->user->id;
            if($apply->save()){
                return $apply;
            }
            return false;
        }
    }
}
